package com.hdaccp.hadoop.mr3;

import com.hdaccp.hadoop.mr2.TwoNum;
import com.hdaccp.hadoop.mr2.TwoNumApp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;
import java.net.URI;

/**
 * 自定义分区
 */
public class MusicApp {
    public static class MusicMapper extends Mapper<LongWritable,Text,Text,Text>
    {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString() ;
            String[] ss =  line.split("\t") ; ;
            String songName = ss[0] ;
            String gender = ss[2] ;

            System.err.println("mapper....");


          //  context.write(new Text(songName),new Text(gender));
            context.write(new Text(gender),new Text(songName));
        }
    }

    public static class MusicReducer extends Reducer<Text,Text,Text,Text>
    {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            System.err.println("reduce....");


            for(Text t:values){
                context.write(key,t);
            }
        }
    }

    public static class MusicPartitioner extends Partitioner<Text,Text>
    {
        /**
         *
         * @param text  键
         * @param text2 value
         * @param i  任务数
         * @return
         */
        @Override
        public int getPartition(Text text, Text text2, int i) {
            String key = text.toString() ;

            System.out.println("key = " + key + " value=" + text2.toString() + " i=" + i);


            if("man".equals(text.toString())){
                    return 0 ;
            }
            else if("woman".equals(text.toString())){
                return 1 ;
            }
            return 2;
        }
    }

    public static void main(String[] args) throws  Exception{
        if(args.length<=0){
            System.err.print("请配置参数");
            System.exit(1);
        }


        //1.读取配置文件
        Configuration cfg = new Configuration() ;
        //2.产生作业
        Job job = Job.getInstance(cfg,"mr3.job1") ;
        //3.配置作业
        job.setJarByClass(MusicApp.class);
        //4.1.配置作业 之 mapper
        job.setMapperClass(MusicMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //4.2配置作业 之 mapper 目标文件
        FileInputFormat.setInputPaths(job,new Path(args[0]));

        //5.1.配置作业 之 reducer
        /*job.setReducerClass(MusicReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);*/
        Path pathout = new Path(args[1]) ;
        //得到hdfs的文件系统
     //   FileSystem fileSystem = FileSystem.get(new URI(cfg.get("fs.defaultFS")),cfg,"root") ;

        FileSystem fileSystem = FileSystem.get(cfg) ;

        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true) ;
        }



        //设置定定义分区
        job.setPartitionerClass(MusicPartitioner.class);
        job.setNumReduceTasks(3);
        FileOutputFormat.setOutputPath(job,pathout);



        //6 提交作业

        System.exit(job.waitForCompletion(true)?0:1) ;
    }
}
